import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		user: null,
		//获取个人中心的个人数据时要用到userInfo
		userInfo: {}
	},
	mutations: {
		getInfo(state, userInfo){
			state.userInfo = userInfo
			// 缓存用户信息
			uni.setStorageSync('userInfo',userInfo)
		},
		login(state, user) {
			state.user = user
			// 缓存账户密码
			uni.setStorageSync('user', user)
		},
		logout(state) {
			state.user = null
			state.userInfo = {}
			uni.clearStorageSync()
		}
	},
	actions: {
		autoLogin(store) {
			// 判断本地是否有账号信息，如果有，就自动重新登录
			if (store.getters.user && store.getters.user.username && store.getters.user.password) {
				const params = {
					username: store.getters.user.username,
					password: store.getters.user.password
				}
				uni.showLoading({
					title: '自动登录中..'
				})
				store.dispatch('login', params).then(response => {
					uni.hideLoading()
						//如果登录成功
							//本地存token存用户信息存密码
							uni.setStorageSync('uniIdToken', response.result.token)
							// 存到vuex里和本地存储
							store.commit('login', params)
							store.commit('getInfo', response.result.userInfo)
							uni.reLaunch({
								url: '/pages/take/take',
							})
					}).catch(
						()=>{
							uni.hideLoading()
							uni.showToast({
								title: '会话过期，请重新登录！',
								icon: 'none'
							})
							uni.reLaunch({
								url: '/pages/other/login'
							})
						}
					)
			} else {
				// 如果本地没有账号信息，就提示用户必须登录
				uni.showModal({
					title: '未登录',
					content: '您未登录，需要登录后才能继续',
					showCancel: false,
					confirmText: '确定',
					success: res => {
						if (res.confirm) {
							uni.reLaunch({
								url: '/pages/other/login'
							})
						}
					}
				})
			}
		},
		login({ commit,dispatch }, params) {
			//清理token
			uni.clearStorageSync()
			const { username, password } = params
			return new Promise((resolve, reject) => {
				uniCloud.callFunction({
					name: 'user-center',
					data: {
						action: 'login',
						params: {
							username: username,
							password: password
						}
					}
				}).then(response => {
					var { result } = response
					//如果没有登录成功
					if(result.code !== 0){
						uni.showToast({
							title: result.msg,
							icon: 'none'
						})
						reject(result.msg)
					}else{
						//存token、密码用户信息
						uni.setStorageSync('uniIdToken', response.result.token)
						commit('login', params)
						commit('getInfo', response.result.userInfo)
						resolve(response)
					}
				}).catch(err => {
					reject(err)
				})
			})
		},
		logout(store) {
			uniCloud.callFunction({
				name: 'user-center',
				data: {
					action: 'logout',
				},
				success:(e)=> {
					store.commit("logout")
					uni.reLaunch({
						url:"/pages/other/login"
					})
				}
			})
		}		
	},
	getters: {
		user: state => {
			if (state.user) {
				return state.user
			}
			return uni.getStorageSync('user')
		},
		userInfo: state => {
			if (state.userInfo) {
				return state.userInfo
			}
			return uni.getStorageSync('userInfo')
		}
	}
})
export default store
